/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : WpDivisionOneImpl.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Sep 30, 2011  mycodetalks			       Created
 */
package net.lwf.handler.wp.helper.general;

import java.util.Map;

import net.lwf.entity.QuestionInfo;
import net.lwf.handler.wp.helper.IWordProblem;
import net.lwf.hibernate.entity.wp.WpData;
import net.lwf.number.util.RandomNumberGenerator;

/**
 * Generates the data where in the remainder of two generated values is always zero.
 */
public class WpDivisionOneImpl extends ArithmaticWp implements IWordProblem {

	public QuestionInfo generate(WpData wpdata) {
		
		Map<Integer, String[]> valuedata = extractMultipleData(wpdata.getDataInput());
		int[] data = this.process(valuedata.get(new Integer(1)), valuedata.get(new Integer(2)));
		
		String question = wpdata.getProblem();
		//Compute the answer
		String answer = String.valueOf((data[0] / data[1]));
		
		String[] svalues = {String.valueOf(data[0]), String.valueOf(data[1])};
		
		return setQuestionInfo(question, wpdata.getReplacements(), svalues, answer, false);
	}
	
	private int[] process(String[] data1, String[] data2) {
		int val1 = RandomNumberGenerator.generate(Integer.parseInt(data1[0]), Integer.parseInt(data1[1]));
		int val2 = RandomNumberGenerator.generate(Integer.parseInt(data2[0]), Integer.parseInt(data2[1]));
		while(val2 > val1) {
			val2 = RandomNumberGenerator.generate(Integer.parseInt(data2[0]), Integer.parseInt(data2[1]));
		}
		int remainder = val1%val2;
		while(remainder != 0) {
			val1 = RandomNumberGenerator.generate(Integer.parseInt(data1[0]), Integer.parseInt(data1[1]));
			val2 = RandomNumberGenerator.generate(Integer.parseInt(data2[0]), Integer.parseInt(data2[1]));
			while(val2 > val1) {
				val2 = RandomNumberGenerator.generate(Integer.parseInt(data2[0]), Integer.parseInt(data2[1]));
			}
			remainder = val1%val2;
		}
		int[] finaldata = {val1, val2};
		return finaldata;
	}
	
}
